<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/2/13
 * Time: 15:58
 */

namespace app\admin\controller;


use app\admin\model\Participant;
use app\admin\model\VoteList as AdminModel;
use think\Config;
use think\Request;


class VoteList extends Admin
{
    protected $code_name = '投票列表';

    public function set_param(){
        parent::set_param();
        $this->assign('code','vote_list');
        $this->assign('code_name',$this->code_name);
        $this->admin_model = new AdminModel();
    }

    public function index(){
        $params = $this->request->param();
        if(empty($params['participant_id'])){
            $this->error('缺少参加人员id');
        }
        if(empty($params['active_id'])){
            $this->error('缺少活动id');
        }
        $where = ['participant_id'=>$params['participant_id'],'active_id'=>$params['active_id']];
        $search_type = empty($params['search_type'])?'':$params['search_type'];
        $keywords = empty($params['keywords'])?'':$params['keywords'];
        if(!empty($keywords)){
            $where[$search_type] = ['like','%'.$keywords.'%'];
        }

        $list = $this->admin_model->where($where)->order('create_time desc')
            ->paginate(10,false,['query'=>['participant_id'=>$params['participant_id'],'active_id'=>$params['active_id'],'search_type'=>$search_type,'keywords'=>$keywords]]);

        return view('',[
            'list'=>$list,
            'page'=>$list->render(),
            'participant_name'=>Participant::where('user_id',$params['participant_id'])->where('active_id',$params['active_id'])->value('nickname'),
            'pass_way'=>Config::get('pass_way'),
            'sites'=>Config::get('site'),
            'participant_id'=>$params['participant_id'],
            'search_type'=>$search_type,
            'keywords'=>$keywords,
            'active_id'=>$params['active_id']
        ]);
    }

    public function change_status(){
        $param = $this->request->param();
        $model = $this->admin_model->where('id',$param['id'])->find();
        if($model){
            $model->status = $param['status'];
            $model->save();
            if($param['status']){
                $text = '已设为有效';
            }else{
                $text = '已设为无效';
            }
            return json(['code'=>0,'message'=>$text]);
        }
        return json(['code'=>177,'message'=>'找不到该记录']);
    }

    public function all_change(){
        $param = $this->request->param();
        if(empty($param['participant_id'])){
            return json(['code'=>199,'message'=>'被投票人id丢失']);
        }
        if(empty($param['search_type'])){
            return json(['code'=>199,'message'=>'查询类型丢失']);
        }
        if(empty($param['keywords'])){
            return json(['code'=>199,'message'=>'查询关键字丢失']);
        }
        if(empty($param['status']) && $param['status'] != 0){
            return json(['code'=>199,'message'=>'修改状态丢失']);
        }
        $where['participant_id'] = $param['participant_id'];
        $where[$param['search_type']] = ['=',$param['keywords']];

        $res = $this->admin_model->where($where)->update(['status'=>$param['status']]);
        return json(['code'=>0,'message'=>'更新满足条件的'.$res.'条记录']);
    }
}